#include "irm.h"

static int moc_argc = 1;
static char* moc_argv[] = { "no value" };


namespace ophies
{

	


IrmMain::IrmMain()
: QCoreApplication(moc_argc, moc_argv)
{
	init();
}

IrmMain::IrmMain(int argc, char *argv[])
: QCoreApplication(argc, argv)
{
	init();
}

IrmMain::~IrmMain()
{
	flush();
	
	QCoreApplication::quit();
	QCoreApplication::exit(0);
}

void IrmMain::init()
{
	connect(this->parent(), SIGNAL(aboutToQuit()),
		this, SLOT(onAboutToQuit()));

	unsigned int mtime = 60 * 60 * 24 * 365;
	mtime = mtime * 1000; // ms
	QCoreApplication::processEvents( 
		QEventLoop::AllEvents, 
		mtime );// 1 year
		// 30);
}

void IrmMain::onAboutToQuit()
{
	assert(false);
}

void IrmMain::listBase(QString cname, RequestCallback* callback)
{
	ObjectRequest *req = new ObjectRequest(cname, callback, parent());
	req->listObject();
	req->deleteLater();
}

void IrmMain::listBase(QString cname, RequestCallback* callback, Search &search)
{
	ObjectRequest *req = new ObjectRequest(cname, callback, &search, this);
	req->listObject();
	req->deleteLater();
}


void IrmMain::getBase(QString cname, QString key, RequestCallback* callback)
{
	ophies::ObjectRequest *request = new ophies::ObjectRequest(cname, callback, this);
	request->getObject(key);
	request->deleteLater();
}


void IrmMain::listDcmStudies(RequestCallback *callback)
{
	listBase("dcm_studies", callback);
}

void IrmMain::listDcmStudies(RequestCallback *callback, SearchDcmStudies &search)
{
	listBase("dcm_studies", callback, search);
}

void IrmMain::listDcmSeries(RequestCallback *callback)
{
	listBase("dcm_series", callback);
}

void IrmMain::listDcmSeries(RequestCallback *callback, SearchDcmSeries &search)
{
	listBase("dcm_series", callback, search);
}


void IrmMain::listDcmInstances(RequestCallback *callback)
{
	listBase("dcm_instances", callback);
}

void IrmMain::listDcmInstances(RequestCallback *callback, SearchDcmInstances &search)
{
	listBase("dcm_instances", callback, search);
}


void IrmMain::listDocsets(RequestCallback *callback)
{
	listBase("docsets", callback);
}

void IrmMain::listDocsets(RequestCallback *callback, SearchDocsets &search)
{
	listBase("docsets", callback, search);
}


void IrmMain::listPatients(RequestCallback *callback)
{
	listBase("patients", callback);
}

void IrmMain::listPatients(RequestCallback *callback, SearchPatients &search)
{
	listBase("patients", callback, search);
}





///////////////////////////////////////////////////////////////////////////
// get object
void IrmMain::getDcmStudy(QString dcm_study_key, RequestCallback* callback)
{
	getBase("dcm_studies", dcm_study_key, callback);
}

void IrmMain::getDcmSeries(QString dcm_series_key, RequestCallback* callback)
{
	getBase("dcm_series", dcm_series_key, callback);
}

void IrmMain::getDcmInstance(QString dcm_instance_key, RequestCallback* callback)
{
	getBase("dcm_instances", dcm_instance_key, callback);
}

void IrmMain::getDocset(QString docset_key, RequestCallback* callback)
{
	getBase("docsets", docset_key, callback);
}

void IrmMain::getPatient(QString patient_key, RequestCallback* callback)
{
	getBase("patients", patient_key, callback);
}


}